<?php
namespace database;

use database\DbHelper;
use utils\PathTool;
use model\DnFileInf;

class DnFile
{
	function __construct()
	{
	}
	
	function Add(&$inf/*DnFileInf*/)
	{
		$sb = "insert into down3_files(
			 f_id
			,f_uid
			,f_mac
			,f_nameLoc
			,f_pathLoc
			,f_fileUrl
			,f_lenSvr
			,f_sizeLoc
			,f_sizeSvr
			,f_fdTask
			) values (
			 :f_id
			,:f_uid
			,:f_mac
			,:f_nameLoc
			,:f_pathLoc
			,:f_fileUrl
			,:f_lenSvr
			,:f_sizeLoc
			,:f_sizeSvr
			,:f_fdTask
			)";
	
		$db = new DbHelper();
		$cmd = $db->prepare_utf8( $sb );
		
		$db->addString($cmd, ":f_id"     , $inf->id);
		$db->addString($cmd, ":f_uid"    , $inf->uid);
		$db->addString($cmd, ":f_mac"    , $inf->mac);
		$db->addString($cmd, ":f_nameLoc", $inf->nameLoc);
		$db->addString($cmd, ":f_pathLoc", $inf->pathLoc);
		$db->addString($cmd, ":f_fileUrl", $inf->fileUrl);
		$db->addInt   ($cmd, ":f_lenSvr" , $inf->lenSvr);
		$db->addString($cmd, ":f_sizeLoc", $inf->sizeLoc);
		$db->addString($cmd, ":f_sizeSvr", $inf->sizeSvr);
		$db->addBool  ($cmd, ":f_fdTask" , $inf->fdTask);
		
		$cmd->execute();
	}
	
	function Delete($fid,$uid)
	{
		$sql = "delete from down3_files where f_id=:f_id and f_uid=:uid";
		$db = new DbHelper();
		$cmd = $db->GetCommand($sql);
		$db->addString($cmd,":f_id", $fid);
		$db->addString($cmd,":uid", $uid);
		$db->ExecuteNonQuery($cmd);
	}
	
	function process($id, $uid, $sizeLoc,$perLoc)
	{
		$sql = "update down3_files set f_sizeLoc=:sizeLoc,f_perLoc=:perLoc where f_id=:f_id and f_uid=:f_uid;";
		$db = new DbHelper();
		$cmd = $db->prepare_utf8($sql);
		$cmd->bindParam(":sizeLoc", $sizeLoc);
		$cmd->bindParam(":perLoc", $perLoc);
		$cmd->bindParam(":f_id", $id);
		$cmd->bindValue(":f_uid", $uid);
		$db->ExecuteNonQuery($cmd);
	}
	
	static function Clear()
	{
		$db = new DbHelper();
		$db->ExecuteNonQueryTxt("delete from down3_files;");
	}
	
	function all_uncmp($uid)
	{
		$sql = "select
				 f_id
				,f_nameLoc
				,f_pathLoc
				,f_perLoc
				,f_sizeSvr
				,f_fdTask
				from down3_files
				where f_uid=:uid";
		$db = new DbHelper();
		$cmd = $db->prepare_utf8($sql);
		$cmd->bindValue(":uid", $uid);
		$ret = $db->ExecuteDataSet($cmd);
		$files = array();
	
		foreach($ret as $row)
		{
			$f            = new DnFileInf();
			$f->id 		  = $row["f_id"];
			$f->nameLoc	  = $row["f_nameLoc"];
			$f->pathLoc   = $row["f_pathLoc"];
			$f->perLoc 	  = $row["f_perLoc"];
			$f->sizeSvr	  = $row["f_sizeSvr"];
			$f->fdTask	  = (bool)$row["f_fdTask"];
			$f->nameLoc   = PathTool::url_encode($f->nameLoc);
			$f->pathLoc   = PathTool::urlencode_path($f->pathLoc);
			$files[]      = $f;
		}
		$json = json_encode($files);
		$json = PathTool::url_decode($json);//还原汉字
		return $json;
	}
	
	function all_complete($uid)
	{
		$sql = "select
		 up7_files.f_id
		,up7_files.f_fdTask
		,up7_files.f_nameLoc
		,up7_files.f_sizeLoc
		,up7_files.f_lenSvr
		,up7_files.f_pathSvr
		,up7_files.f_blockPath
		,f_fileCount
		from up7_files
		left join up7_folders on up7_folders.f_id=up7_files.f_id
		where up7_files.f_uid='$uid' and up7_files.f_deleted=0 and up7_files.f_complete=1 and up7_files.f_scan=1 and up7_files.f_fdChild=0";
		//
		$db = new DbHelper();
		$cmd = $db->prepare_utf8($sql);
		$ret = $db->ExecuteDataSet($cmd);
		$files = array();
	
		foreach($ret as $row)
		{
			$f            = new DnFileInf();
			$f->id        = PathTool::guid();
			$f->f_id      = $row["f_id"];
			$f->fdTask    = (bool)$row["f_fdTask"];
			$f->nameLoc   = $row["f_nameLoc"];
			$f->sizeLoc   = "0byte";
			$f->sizeSvr   = $row["f_sizeLoc"];
			$f->lenSvr    = $row["f_lenSvr"];
			$f->pathSvr   = $row["f_pathSvr"];
			$f->blockPath = $row["f_blockPath"];
			$f->fileCount = intval($row["f_fileCount"]);
			$f->nameLoc   = PathTool::url_encode($f->nameLoc);
			$f->pathSvr   = PathTool::urlencode_path($f->pathSvr);
			$f->blockPath = PathTool::urlencode_path($f->blockPath);
			$f->pathSvr   = str_replace("\\\\", "/", $f->pathSvr);
			$f->blockPath = str_replace("\\\\", "/", $f->blockPath);
			$files[] = $f;
		}
		$json = json_encode($files);
		$json = PathTool::url_decode($json);//还原汉字
		return $json;
	}
	
	function guid()
	{
		$ret = "";
		if (function_exists('com_create_guid'))
		{
			$ret = com_create_guid();
		}
		else
		{
			mt_srand((double)microtime()*10000);//optional for php 4.2.0 and up.
			$charid = strtoupper(md5(uniqid(rand(), true)));
			$hyphen = chr(45);// "-"
			$uuid = chr(123)// "{"
			.substr($charid, 0, 8).$hyphen
			.substr($charid, 8, 4).$hyphen
			.substr($charid,12, 4).$hyphen
			.substr($charid,16, 4).$hyphen
			.substr($charid,20,12)
			.chr(125);// "}"
			$ret = $uuid;
		}
		$ret = str_replace("{","",$ret);
		$ret = str_replace("}","",$ret);
		$ret = str_replace("-","",$ret);
		$ret = strtolower($ret);
		return $ret;
	}
}
?>